package com.shaozj.minio.controller;

import com.shaozj.core.result.Result;
import com.shaozj.minio.utils.MinioUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "06.MinIO")
@RestController
@RequestMapping("/api/v1/minio")
@AllArgsConstructor
public class MinioController {

    private final MinioUtil minioUtil;

    @Operation(summary = "上传文件")
    @PostMapping("/upload")
    public Result upload(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        minioUtil.upload(file, fileName);
        return Result.success(minioUtil.getFileUrl(fileName));
    }

    @Operation(summary = "下载文件")
    @GetMapping("/download")
    public void download(@Parameter(description = "文件名") String fileName, @Parameter(description = "响应体")HttpServletResponse response) {
        minioUtil.download(fileName, response);
    }

    @Operation(summary = "预览文件")
    @GetMapping("/preview")
    public Result preview(@Parameter(description = "文件名")String fileName) {
        return Result.success(minioUtil.getFileUrl(fileName));
    }

    @Operation(summary = "删除文件")
    @DeleteMapping("/delete")
    public Result delete(@Parameter(description = "文件名") String fileName) {
        minioUtil.delete(fileName);
        return Result.success();
    }
}
